﻿//加入购物车
function AddToCart(dishid, dishname, price) {
    var temp = "[";
    if ($.cookie("dishcart") != null) {//购物车不为空
        var cart = $.cookie("dishcart");
     
        var flg = false;
        var objcart = eval(cart);
        for (var i = 0; i < objcart.length; i++) {
            var count = parseInt(objcart[i].count);
            var _price = parseFloat(objcart[i].price);
            if (objcart[i].dishid == dishid) {
                var newcount = parseInt(count) + 1;
                var newprice = parseFloat(_price) + price;
                temp += "{\"dishid\":\"" + objcart[i].dishid + "\",\"count\":\"" + newcount + "\",\"dishname\":\"" + objcart[i].dishname + "\",\"price\":\"" + newprice + "\"},";
                flg = true;
            } else {
                temp += "{\"dishid\":\"" + objcart[i].dishid + "\",\"count\":\"" + count + "\",\"dishname\":\"" + objcart[i].dishname + "\",\"price\":\"" + objcart[i].price + "\"},";
            }
        }
        if (flg == false) {
            temp += "{\"dishid\":\"" + dishid + "\",\"count\":\"1\",\"dishname\":\"" + dishname + "\",\"price\":\"" + price + "\"},";//新加入
        }
    } else {
        temp = "[{\"dishid\":\"" + dishid + "\",\"count\":\"1\",\"dishname\":\"" + dishname + "\",\"price\":\""+price+"\"}";
    }
    temp += "]";
    $.cookie("dishcart", temp, { expires: 1 });
    BuildCart();
}

//从cookie中取数据构建购物车
function BuildCart() {
    var sumprice = 0;
    var carttable = "<table><tr><th>菜名</th><th>价格</th><th>数量</th><th></th></tr>";
    if ($.cookie("dishcart") != null) {//购物车不为空
        var cart = $.cookie("dishcart");
       
        $("#hfdishs").val(cart);
        var objcart = eval(cart);
        var _tr="";
        for (var i = 0; i < objcart.length; i++) {
            _tr += "<tr>";
            _tr += "<td>" + objcart[i].dishname + "</td>";
            _tr += "<td>" + objcart[i].price + "</td>";
            _tr += "<td><input type=\"button\" value=\"-\" onclick=modifycount(" + objcart[i].dishid + ",'reduce') /> " + objcart[i].count + " <input type=\"button\" value=\"+\" onclick=modifycount(" + objcart[i].dishid + ",'add') /></td>";
            _tr += "<td><input type=\"button\" value=\"删除\" onclick=modifycount(" + objcart[i].dishid + ",'delete') /></td>";
            _tr += "</tr>"
            sumprice += objcart[i].price * objcart[i].count;
        }
        _tr += "<tr><td colspan=3></td><input type=\"button\" value=\"提交订单\" onclick=submitOrder() /></tr>";
        carttable += _tr;
    } else {
        carttable = "<tr><td colspan=3>您还没点菜!</td></tr>";
    }
    carttable += "</table>";
    $("#shopcart").html(carttable);
    $("#sumprice").html(sumprice);
}

//增加\减少\删除数量
function modifycount(dishid, type) {
    var temp = "[";
    if ($.cookie("dishcart") != null) {
        var cart = $.cookie("dishcart");
        var objcart = eval(cart);
        for (var i = 0; i < objcart.length; i++) {
            if (objcart[i].dishid == dishid) {
                var count = parseInt(objcart[i].count);
                if (type == "add") {
                    var newcount = parseInt(count) + 1;
                    temp += "{\"dishid\":\"" + objcart[i].dishid + "\",\"count\":\"" + newcount + "\",\"dishname\":\"" + objcart[i].dishname + "\",\"price\":\"" + objcart[i].price + "\"},";
                } else if (type == "reduce") {
                    var newcount = parseInt(count) - 1;
                    if (newcount < 1) {
                        temp += "";
                    } else {
                        temp += "{\"dishid\":\"" + objcart[i].dishid + "\",\"count\":\"" + newcount + "\",\"dishname\":\"" + objcart[i].dishname + "\",\"price\":\"" + objcart[i].price + "\"},";
                    }
                } else if (type == "delete") {
                    temp += "";
                }
            } else {
                temp += "{\"dishid\":\"" + objcart[i].dishid + "\",\"count\":\"" + objcart[i].count + "\",\"dishname\":\"" + objcart[i].dishname + "\",\"price\":\"" + objcart[i].price + "\"},";
            }
        }
    }
    temp += "]";
    $.cookie("dishcart", temp, { expires: 1 });
    BuildCart();
}

//提交订单
function submitOrder() {
    
    window.location.href = "order.aspx";
}